//
//  TicketOrderDetailController.m
//  lv678
//
//  Created by apple on 15/7/19.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  订单信息填写

#import "TicketOrderDetailController.h"
#import "AFNetworking.h"
#import "GZOrderDetail.h"
#import "TicketSubmitOrderController.h"
@interface TicketOrderDetailController ()
@property (weak, nonatomic) IBOutlet UILabel *productNameLbl;
@property (weak, nonatomic) IBOutlet UILabel *priceLbl;
- (IBAction)numDown:(id)sender;
- (IBAction)numUp:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *ticketNum;
@property (weak, nonatomic) IBOutlet UITextField *contacts;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *email;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UILabel *commentLabel;
@property (weak, nonatomic) IBOutlet UITextView *commentView;

@property(nonatomic)NSInteger priceInteger;
@property(nonatomic,strong)GZOrderDetail *orderDetailData;
@end

@implementation TicketOrderDetailController
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置代理
    _ticketNum.delegate       = self;
    _contacts.delegate        = self;
    _phoneNumber.delegate     = self;
    _email.delegate           = self;
    _commentView.delegate     = self;

    self.navigationItem.title = @"订单填写";
    _productNameLbl.text      = self.productName;
    NSString *pricestr        = [[[NSNumberFormatter alloc]init]stringFromNumber:self.price];
    _priceLbl.text            = pricestr;
    [self setCommentStyle];
    [self setSubmitBtnStyle];
    
    [_submitBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    
}
//设置留言板样式
- (void)setCommentStyle{
    _commentView.layer.borderWidth   = 1;
    _commentView.layer.borderColor   = [[UIColor lightGrayColor]CGColor];
    _commentView.layer.cornerRadius  = 6;
    _commentView.layer.masksToBounds = YES;
    _commentView.frame               = CGRectMake(18, 220, 280, 90);

}

//设置提交按钮的样式
- (void)setSubmitBtnStyle{
    _submitBtn.layer.cornerRadius  = 6;
    _submitBtn.layer.masksToBounds = YES;
    _submitBtn.frame               = CGRectMake(10, 380, 300, 40);

}
//点击空白区域隐藏键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
//点击键盘上的return隐藏键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
//留言区域文字的显示和隐藏
- (void)textViewDidChange:(UITextView *)textView{
    if (_commentView.text == nil) {
        _commentLabel.hidden = NO;
    }else{
        _commentLabel.hidden = YES;
    }
}
//价格随旅游票的数量改变
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSInteger num            = [_ticketNum.text intValue];
    self.priceInteger        = [_price integerValue];
    NSInteger ticketPriceSum = self.priceInteger*num;
    NSString *priceSum       = [NSString stringWithFormat:@"%ld",(long)ticketPriceSum];
    _priceLbl.text           = priceSum;
}
//减少旅游票数量
- (IBAction)numDown:(id)sender {
    NSInteger num = [_ticketNum.text intValue];
    num           = num-1;
    //旅游票数量不能小于1
    if (num < 1) {
        num = 1;
    }
    self.priceInteger        = [_price integerValue];
    NSInteger ticketPriceSum = self.priceInteger*num;
    NSString *ticketAccount  = [NSString stringWithFormat:@"%ld",(long)num];
    NSString *priceSum       = [NSString stringWithFormat:@"%ld",(long)ticketPriceSum];
    _ticketNum.text          = ticketAccount;
    _priceLbl.text           = priceSum;
}

//增加旅游票数量
- (IBAction)numUp:(id)sender {
    NSInteger num            = [_ticketNum.text intValue];
    num                      = num+1;
    self.priceInteger        = [_price integerValue];
    NSInteger ticketPriceSum = self.priceInteger*num;
    NSString *ticketAccount  = [NSString stringWithFormat:@"%ld",(long)num];
    NSString *priceSum       = [NSString stringWithFormat:@"%ld",(long)ticketPriceSum];
    _ticketNum.text          = ticketAccount;
    _priceLbl.text           = priceSum;
}

- (void)submit{
    NSString *path                                    = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *tempDic                             = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *url                                     = [tempDic objectForKey:@"travelTicket"];
    AFHTTPRequestOperationManager *manager            = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer                        = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    manager.requestSerializer                         = [AFJSONRequestSerializer serializer];
    NSDictionary *data                                = @{@"serviceName":@"addorder",@"queryParameters":@{@"words":_commentView.text,@"email":_email.text,@"userId":@"3",@"name":_contacts.text,@"orderAccount":_ticketNum.text,@"tid":_tid,@"telephone":_phoneNumber.text}};
    [manager POST:url parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _orderDetailData = [GZOrderDetail orderWithDict:[responseObject valueForKey:@"dataList"]];
        TicketSubmitOrderController *orderDetail = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"orderDetail"];
        orderDetail.productName   = _orderDetailData.productName;
        orderDetail.ticketAccount = _orderDetailData.orderAccount;
        orderDetail.contactsName  = _orderDetailData.name;
        orderDetail.phoneNum      = _orderDetailData.telephone;
        orderDetail.orderNum      = _orderDetailData.orderNum;
        orderDetail.orderSum      = _orderDetailData.orderSum;
        orderDetail.createTime    = _orderDetailData.createtime;
        [self.navigationController pushViewController:orderDetail animated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error.....");
    }];
    
}


@end
